/*=============================================================================
	CommonRender.cpp: Crytek Common render helper functions and structures declarations.
	Copyright (c) 2001 Crytek Studios. All Rights Reserved.

	Revision history:
		* Created by Kenzo ter Elst

=============================================================================*/

#include "StdAfx.h"

#include "IntroMovieRenderer.h"
#include "ILocalizationManager.h"





//////////////////////////////////////////////////////////////////////////

CIntroMovieRenderer::CIntroMovieRenderer() : m_pFlashPlayer(0)
{
}

CIntroMovieRenderer::~CIntroMovieRenderer()
{
	SAFE_RELEASE(m_pFlashPlayer);
}

bool CIntroMovieRenderer::Initialize()
{
	SAFE_RELEASE(m_pFlashPlayer);

	m_pFlashPlayer = gEnv->pSystem->CreateFlashPlayerInstance();

	if (m_pFlashPlayer)
	{
		m_pFlashPlayer->Load("libs/ui/usm_player_intro.gfx");
		m_pFlashPlayer->Advance(0.f);

		m_pFlashPlayer->SetFSCommandHandler(this);
		m_pFlashPlayer->SetBackgroundAlpha(0.0f);
		m_pFlashPlayer->Advance(0.0f);

		m_pFlashPlayer->SetVariable("showSubtitle", true);
		m_pFlashPlayer->SetVariable("selectedSubtitle", GetSubtitleChannelForSystemLanguage());

		m_pFlashPlayer->SetVariable("canSkip", false);

		m_pFlashPlayer->Invoke1("setMoviePath", "/Videos/IntroMovies.usm");

		if(ICVar* pCVar = gEnv->pConsole->GetCVar("s_SFXVolume"))
		{
			const int volume = int_round(clamp(pCVar->GetFVal()*100.0f, 0.0f, 100.0f));
			m_pFlashPlayer->Invoke1("vSetVolume", volume);
		}

		m_pFlashPlayer->Invoke1("setFollowUpAnimation", false);
		m_pFlashPlayer->Invoke1("setBlackBackground", true);

		m_pFlashPlayer->Invoke0("vPlay");
	}

	return true;
}

CIntroMovieRenderer::EVideoStatus CIntroMovieRenderer::GetCurrentStatus()
{
	if (m_pFlashPlayer)
	{
		SFlashVarValue val(0.0);
		m_pFlashPlayer->GetVariable("m_status", val);
		EVideoStatus status = (EVideoStatus)int_round(val.GetDouble());
		return status;
	}
	return eVideoStatus_Error;
}

void CIntroMovieRenderer::WaitForCompletion()
{
	bool bFinished = false;
	while (!bFinished)
	{
		EVideoStatus status = GetCurrentStatus();

		switch (status)
		{
		case eVideoStatus_PrePlaying:
		case eVideoStatus_Stopped:
		case eVideoStatus_Error:
		case eVideoStatus_Finished:
			{
				bFinished = true;
			}
			break;
		}
		Sleep(1);
	}	
}

int CIntroMovieRenderer::GetSubtitleChannelForSystemLanguage()
{
	ILocalizationManager::EPlatformIndependentLanguageID language = ILocalizationManager::ePILID_MAX_OR_INVALID;



















































	switch(language)
	{
	case ILocalizationManager::ePILID_English:	return 1; break;
	case ILocalizationManager::ePILID_French:		return 2; break;
	case ILocalizationManager::ePILID_German:		return 3; break;
	case ILocalizationManager::ePILID_Italian:	return 4; break;
	case ILocalizationManager::ePILID_Russian:	return 5; break;
	case ILocalizationManager::ePILID_Polish:		return 6; break;
	case ILocalizationManager::ePILID_Spanish:	return 7; break;
	case ILocalizationManager::ePILID_Turkish:	return 8; break;
	case ILocalizationManager::ePILID_Japanese:	return 9; break;
	case ILocalizationManager::ePILID_Czech:		return 10; break;
	case ILocalizationManager::ePILID_ChineseT:	return 11; break;
	default:																		return 0; break;
	}
}

//////////////////////////////////////////////////////////////////////////

void CIntroMovieRenderer::LoadtimeUpdate(float deltaTime)
{
	if (m_pFlashPlayer)
	{
		UpdateViewport();
		m_pFlashPlayer->Advance(deltaTime);
	}
}

void CIntroMovieRenderer::LoadtimeRender()
{
	if (m_pFlashPlayer)
		m_pFlashPlayer->Render(true);
}

void CIntroMovieRenderer::UpdateViewport()
{
	if(!m_pFlashPlayer)
		return;

	int videoWidth(m_pFlashPlayer->GetWidth());
	int videoHeight(m_pFlashPlayer->GetHeight());

	const int screenWidth(gEnv->pRenderer->GetWidth());
	const int screenHeight(gEnv->pRenderer->GetHeight());

	const float pixelAR = gEnv->pRenderer->GetPixelAspectRatio();









	const float scaleX((float)screenWidth / (float)videoWidth);
	const float scaleY((float)screenHeight / (float)videoHeight);

	float scale(scaleY);

	/*
	if(flags&eVideoFlag_KeepAspectRatio)
	{
		if (flags&eVideoFlag_CoverFullScreen)
		{
			float videoRatio((float)videoWidth / (float)videoHeight);
			float screenRatio((float)screenWidth / (float)screenHeight);

			if (videoRatio < screenRatio)
				scale = scaleX;
		}
		else
		{
			if (scaleY * videoWidth > screenWidth)
				scale = scaleX;
		}
	}
	else
	*/
	{
		scale = 1.0f;
		videoWidth = screenWidth;
		videoHeight = screenHeight;
	}

	int w(int_round(videoWidth * scale));
	int h(int_round(videoHeight * scale));
	int x((screenWidth - w) / 2);
	int y((screenHeight - h) / 2);

	SetViewportIfChanged(x, y, w, h, 1.0f);


}

void CIntroMovieRenderer::SetViewportIfChanged(const int x, const int y, const int width, const int height, const float pixelAR)
{
	int oldX, oldY, oldWidth, oldHeight = 0;
	float oldAR = 1.0f;
	m_pFlashPlayer->GetViewport(oldX, oldY, oldWidth, oldHeight, oldAR);
	if(x!=oldX || y!=oldY || width!=oldWidth || height!=oldHeight || fabs(oldAR-pixelAR)>FLT_EPSILON)
	{
		m_pFlashPlayer->SetViewport(x, y, width, height, pixelAR);
	}
}

//////////////////////////////////////////////////////////////////////////